package oop.controller.action.test;

import oop.data.Section;
import oop.db.dao.SectionDAO;

import com.mysql.jdbc.StringUtils;

public class EditSectionInfo extends AbstractEditAction {

	@Override
	protected void performDetail() throws Exception {
		try {
			long id = Long.parseLong(request.getParameter("id"));
			Section section = null;
			for (Section s : test.getSections()) {
				if (s.getId() == id) {
					section = s;
					break;
				}
			}
			if (section == null) {
				request.setAttribute("message", "Không tìm thấy mục");
				setNextAction("error");
				return;
			}
			request.setAttribute("section", section);

			String submit = request.getParameter("tsesubmit");
			if (!StringUtils.isNullOrEmpty(submit)) {
				section.setText(request.getParameter("tsetext"));
				section.setOrder(Integer.parseInt(request
						.getParameter("tseorder")));
				SectionDAO.persist(section);

				long testId = Long.parseLong(request.getParameter("tsetest"));
				setNextAction("test.view&id=" + testId);
			}
		} catch (NumberFormatException ex) {
			request.setAttribute("message", "ID không hợp lệ");
			setNextAction("error");
		}
	}

}
